<?php
Helper::load("constructforms");
?>

<form id="frm_db_cols">
    <input type="hidden" name="table_name" value="<?php echo $table_name;?>">
    <input type="hidden" name="app_path" value="<?php echo $app_path ;?>">
    <input type="hidden" name="template_folder" value="<?php echo $template_folder ;?>">
    <table>
        <tr>
            <td>
                <?php echo form_checkbox('Модель записи', 'cbox_rec_model', 0); ?>
            </td>
            <td>
                <?php echo form_edit('record_model_name', 30); ?>
                <button id="btnGenIdents">Сгенерировать идентификаторы</button>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Модель управления', 'cbox_manager_model', 0); ?>
            </td>
            <td>
                <?php echo form_edit('manager_model_name', 30); ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Контроллер', 'cbox_controller', 0); ?>
            </td>
            <td>
                <?php echo form_edit('controller_name', 30); ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Форма редактирования', 'cbox_rec_frm', 0); ?>
            </td>
            <td>
                <?php echo form_edit('rec_form_name', 30); ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Форма просмотра', 'cbox_rec_view_frm', 0); ?>
            </td>
            <td>
                <?php echo form_edit('rec_view_form_name', 30); ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Имя переменной данных', 'cbox_rec_frm_name', 0); ?>
            </td>
            <td>
                <?php echo form_edit('rec_name', 30); ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo form_checkbox('Форма списка', 'cbox_list_frm', 0); ?>
            </td>
            <td>
                <?php echo form_edit('list_form_name', 30); ?>
            </td>
        </tr>

    </table>

    <table>
        <tr>
            <th>Поле</th>
            <th>Тип данных</th>
            <th>Элемент формы</th>
            <th>Подпись</th>
            <th>Связанный список</th>
            <th>Связанная модель</th>
            <th>Функция</th>
        </tr>
        <?php
        foreach ($rows as $row) {
            echo "<tr>";
            echo "<td>";
            echo $row->Field;
            echo "</td>";
            echo "<td>";
            echo $row->Type;
            echo "</td>";
            echo "<td>";
            $type_col = "edit";
            if (substr($row->Type, 3) == "int") {
                $type_col = "select";
            }
            //            if (substr($row->Type, 7) == "varchar") {
            //                $type_col = "select";
            //            }
            //            if (substr($row->Type, 7) == "varchar") {
            //                $type_col = "select";
            //            }
            if (substr($row->Type, 4) == "text") {
                $type_col = "text";
            }
            echo form_combo($row->Field . "_frm_control", $type_col, $lst_frm_types);
            echo "</td>";
            echo "<td>";
            echo form_edit($row->Field . "_lbl", 20);
            echo "</td>";
            echo "<td>";
            echo form_edit($row->Field . "_list", 20);
            echo "</td>";
            echo "<td>";
            echo form_edit($row->Field . "_link", 20);
            echo "</td>";
            echo "<td>";
            echo form_edit($row->Field . "_function", 20);
            echo "</td>";
            echo "</tr>";
        }
        ?>
    </table>

    <button type=submit class=btn>Сгенерировать код</button>

</form>
<div id="str_result"></div>
<script>
    $('#frm_db_cols').submit(function () {
        $.post('/main/genform', $('#frm_db_cols').serialize(),
            function (data) {

                //$('#infomes').html(data);
                str_data = data;

                //alert(str_data);
                $('#str_result').html(str_data);
            });
        return false;
    });

    $("#btnGenIdents").click(function () {
        rec_name = $('#record_model_name').val();
        $('#manager_model_name').val("Manager"+rec_name);
        $('#controller_name').val(rec_name);
        $('#rec_form_name').val("frm_"+rec_name);
        $('#rec_view_form_name').val("view_"+rec_name);
        $('#rec_name').val("record");
        $('#list_form_name').val("list_"+rec_name);
        return false;
        alert('done');
    });

</script>

